package zjitc.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import zjitc.domain.User;
import zjitc.domain.Vo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/quick")
    public String save(){
        System.out.println("Controller savvv");
        return "/success";
    }
    @RequestMapping("/quick2")
    public ModelAndView save2(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("username","zhangsan");
        mv.setViewName("/index");
        return mv;
    }
    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView mv){
        mv.addObject("username","zhangsanaa");
        mv.setViewName("/index");
        return mv;
    }
    @RequestMapping("/quick4")
    public String save4(Model model){
        model.addAttribute("username","博学谷");
        return "/index";
    }
    @RequestMapping("/quick5")
    public String save5(HttpServletRequest request){
       request.setAttribute("username","吱吱吱吱");
        return "/index";
    }
    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello itcast");
    }
    @RequestMapping("/quick7")
    @ResponseBody
    public String save7()  {
        return "hello itheima";
    }
    @RequestMapping("/quick8")
    @ResponseBody
    public String save8()  {
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }
    @RequestMapping("/quick9")
    @ResponseBody
    public String save9() throws JsonProcessingException {
        User user=new User();
        user.setUsername("李一");
        user.setAge(30);
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }
    @RequestMapping("/quick10")
    @ResponseBody
    public User save10(){
        User user=new User();
        user.setUsername("李三");
        user.setAge(30);
        return user;
    }
    @RequestMapping("/quick11")
    @ResponseBody
    public void save11(String username ,int age){
        System.out.println(username);
        System.out.println(age);
    }
    @RequestMapping("/quick12")
    @ResponseBody
    public void save12(User user){
        System.out.println(user);
    }
    @RequestMapping("/quick13")
    @ResponseBody
    public void save13(String[] strs){
        System.out.println(Arrays.asList(strs));
    }
    @RequestMapping("/quick14")
    @ResponseBody
    public void save14(Vo vo){
        System.out.println(vo);
    }
    @RequestMapping("/quick15")
    @ResponseBody
    public void save15(@RequestBody List<User> userList){
        System.out.println(userList);
    }
    @RequestMapping("/quick16")
    @ResponseBody
    public void save16(@RequestParam(value="name",required=false) String username ){
        System.out.println(username);
    }
    @RequestMapping("/quick17/{name}")
    @ResponseBody
    public void save17(@PathVariable(value="name",required=false) String username ){
        System.out.println(username);
    }
    @RequestMapping("/quick18")
    @ResponseBody
    public void save18(Date date){
        System.out.println(date);
    }
    @RequestMapping("/quick19")
    @ResponseBody
    public void save19(@RequestHeader(value="User-Agent") String user_agent){
        System.out.println(user_agent);
    }
    @RequestMapping("/quick20")
    @ResponseBody
    public void save20(@CookieValue(value="JSESSIONID") String jsessionId){
        System.out.println(jsessionId);
    }
}
